package controller;

import java.io.IOException;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class FilmAdminAdd
 */
@WebServlet("/FilmAdmin")
public class FilmAdmin extends HttpServlet {
	private static final long serialVersionUID = 1L;  	
	
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FilmAdmin() {
       
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String add = request.getParameter("add");
		
		if(id!=null)
		{
			String isEdit = request.getParameter("edit");
			Integer id_R = new Integer(id);
			if(Boolean.parseBoolean(isEdit))	// edycja
			{
				try {
					OracleProcedure op = new OracleProcedure(OracleProcedure.SELECT, "FILM");
					op.setColumn("id");
					op.make(true, id_R);
					ResultSet cursor = op.getCursor();
					cursor.next();
					request.setAttribute("Film",Utils.getSingleFilm(cursor));
				
					OracleProcedure op2 = new OracleProcedure(OracleProcedure.SELECT,"Rezyser");
					op2.make(true);
					cursor = op.getCursor();
					request.setAttribute("RezyserList", Utils.prepareMinimalRezyserList(cursor)); 
				} catch (SQLException e) {
					e.printStackTrace();
				}
				RequestDispatcher dispatcher = 
			    		getServletContext().getRequestDispatcher("/restricted/edytuj_film.jsp"); 
			    dispatcher.forward(request, response);
			}
			else	// usuwanie
			{
				OracleProcedure op = new OracleProcedure(OracleProcedure.DELETE,"Film");
				op.make(id_R);
				processRequest(request,response);
			}
		}
		else if(add!=null)	// dodawanie
		{
			if(Boolean.parseBoolean(add))
			{
				OracleProcedure op = new OracleProcedure(OracleProcedure.SELECT, 
						"Rezyser");
				op.make(true);
				ResultSet cursor = op.getCursor();
				request.setAttribute("RezyserList", Utils.prepareMinimalRezyserList(cursor));

				RequestDispatcher dispatcher = getServletContext()
						.getRequestDispatcher("/restricted/dodaj_film.jsp");
				dispatcher.forward(request, response);
			}
		}
		else // add == null, id == null
		{
			OracleProcedure op = new OracleProcedure(OracleProcedure.SELECT,"Film");
			op.make(true);
			ResultSet cursor = op.getCursor();
			request.setAttribute("filmList", Utils.prepareFilmList(cursor)); 
			RequestDispatcher dispatcher = 
		    		getServletContext().getRequestDispatcher("/restricted/index.jsp"); 
		    dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String tytul = (String) request.getParameter("film_tytul");
			String rok = (String) request.getParameter("film_rok");
			Integer rezyser = new Integer(request.getParameter("film_rezyser"));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date date;
			date = (sdf.parse(rok));
			
			String id = (String) request.getParameter("id");
			
			if(id!=null)	// update
			{
				Integer id_r = new Integer(id);
				OracleProcedure op = new OracleProcedure(OracleProcedure.UPDATE,"Film");
				op.make(id_r, tytul, 
						new Date(date.getTime()), rezyser);
			}
			else	// insert
			{
				OracleProcedure op = new OracleProcedure(OracleProcedure.INSERT,"Film");
				op.make(tytul, 
						new Date(date.getTime()), rezyser);
			}
			
			processRequest(request,response);
		} catch (Exception ex) {
			throw new ServletException(ex);
		} finally {
		}
	}

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
		    throws ServletException {
		try {
			response.sendRedirect(getServletContext().getContextPath()+"/restricted/FilmAdmin");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
